package br.progweb.webapp.controller;

import br.progweb.webapp.model.Grupo;
import br.progweb.webapp.service.GrupoManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: luiza
 * Date: 09/09/12
 * Time: 17:13
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/grupoForm*")
public class GrupoFormController extends BaseFormController {
// ------------------------------ FIELDS ------------------------------

    @Autowired
    GrupoManager grupoManager;

// -------------------------- OTHER METHODS --------------------------

    @ModelAttribute
    @RequestMapping(method = RequestMethod.GET)
    protected Grupo getGrupo(HttpServletRequest request, ModelMap model) {
        String grupoId = request.getParameter("id");
        if ((grupoId != null) && !grupoId.equals("")) {
            return grupoManager.get(Long.parseLong(grupoId));
        } else {
            return new Grupo();
        }
    }

    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        // convert java.util.Date
        SimpleDateFormat dateFormat = new SimpleDateFormat(getText("date.format"));
        dateFormat.setLenient(false);

        binder.registerCustomEditor(Date.class, null,
                new CustomDateEditor(dateFormat, true));

        // convert java.lang.Long
        binder.registerCustomEditor(Long.class, null,
                new CustomNumberEditor(Long.class, null, true));
    }

    @RequestMapping(method = RequestMethod.POST)
    public String onSubmit(Grupo grupo, BindingResult result, HttpServletRequest request) throws Exception {
        if (request.getParameter("cancel") != null)
            return "redirect:grupos";


        if (validator != null) { // validator is null during testing
            validator.validate(grupo, result);

            if (result.hasErrors()) {
                return "grupoForm";
            }
        }

        log.debug("entrando metodo 'onSubmit' ");

        if (request.getParameter("delete") != null) {
            grupoManager.removeGrupo(grupo);
            request.getSession().setAttribute("message",
                    getText("contato.deleted", grupo.getDescricao()));
        } else {
            grupoManager.saveGrupo(grupo);
            request.getSession().setAttribute("message",
                    getText("contato.saved", grupo.getDescricao()));
        }

        return "redirect:grupos";
    }
}
